package app.qrcode.browser.hearttoheart.adapters

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import app.qrcode.browser.hearttoheart.R

/**
 * Created by DomenCai on 2022/4/1.
 */
class PersonalAdapter : RecyclerView.Adapter<PersonalAdapter.PersonalViewHolder>() {

    private val personalList: MutableList<Personal> = mutableListOf()

    @SuppressLint("NotifyDataSetChanged")
    fun updateList(newList: ArrayList<Personal>) {
        personalList.clear()
        personalList.addAll(newList)
        notifyDataSetChanged()
    }

    inner class PersonalViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val personalText: TextView = view.findViewById(R.id.p_TV)
        val personalPic: ImageView = view.findViewById(R.id.p_head)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PersonalViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_personal, parent, false)
        return PersonalViewHolder(view)
    }

    override fun onBindViewHolder(holder: PersonalViewHolder, position: Int) {
        val personal = personalList[position]
        holder.personalText.text = personal.personalText
        holder.personalPic.setImageResource(personal.personalPic)
    }

    override fun getItemCount(): Int = personalList.size
}